package com.zone.editor.shape;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.util.Log;

import com.zone.editor.pixel.image.ColorItem;
import com.zone.editor.pixel.image.ColorItemPool;
import com.zone.editor.pixel.image.Template;
import com.zone.editor.utils.NumberUtil;
import com.zone.editor.utils.RomUtil;
import com.zone.sdk.isd.util.ISDColorUtil;

import java.util.List;

public class PixelBrushShape extends BrushShape {

    private static final String TAG = PixelBrushShape.class.getSimpleName();

    ColorItemPool colorItem;
    private int radius;
    private int widthNum;
    private int heightNum;
    private ShapeBuilder shapeBuilder;
    //ISD尾灯颜色中间区域只能显示红色，矩形区域画笔颜色定死
    private RectF staticRect;
    private int staticColor;
    private Template template;
    private boolean stop = false;

    public PixelBrushShape(ColorItemPool colorItem, int radius, int widthNum, int heightNum, ShapeBuilder shapeBuilder, RectF rect, int color, Template template) {
        this.colorItem = colorItem;
        this.radius = radius;
        this.widthNum = widthNum;
        this.heightNum = heightNum;
        this.shapeBuilder = shapeBuilder;
        this.staticRect = rect;
        this.staticColor = color;
        this.template = template;
    }

    @Override
    public String getTag() {
        return TAG;
    }

    @Override
    public void draw(Canvas canvas, Paint paint) {
        List<ColorItemPool.Point> points = colorItem.getColorData();
        for (int i = 0; i < points.size(); i++) {
            ColorItemPool.Point point = points.get(i);
            ColorItem item = point.getColorItem();
            paint.setColor(item.getColor());
            if (RomUtil.isMiui()){
                //            canvas.drawRect(item.getWidth(), item.getHeight() - template.getDensity(), item.getWidth() + radius * template.getTimes(), item.getHeight() + radius * template.getTimes() - template.getDensity(), paint);
                canvas.drawRect(item.getWidth(), item.getHeight() - template.getDensity() - 2, item.getWidth() + radius * template.getTimes()+1, item.getHeight() - template.getDensity() - 2 + radius * template.getTimes(), paint);
            }else {
//                canvas.drawRect(item.getWidth(), item.getHeight() - template.getDensity() - 2, item.getWidth() + radius * template.getTimes()+1, item.getHeight() - template.getDensity() - 2 + radius * template.getTimes(), paint);
                canvas.drawRect(item.getWidth()-2, item.getHeight() - template.getDensity() -2, item.getWidth() + radius * template.getTimes() -1, item.getHeight() + radius * template.getTimes() - template.getDensity() - 1, paint); }
        }
    }

    @Override
    public void moveShape(float xf, float yf) {

        ColorItem color = new ColorItem();
        int length = widthNum * heightNum;
        int pixelIndex = 0;
        int h;
        int w;
        if (length >= template.getTemplateColor().length) {
            //放大：布局与模板比
            h = (int) (yf / template.getTimes());
            w = (int) (xf / template.getTimes());
        } else {
            //缩小：布局与模板比
            h = (int) (yf * template.getTimes());
            w = (int) (xf * template.getTimes());
        }
        pixelIndex = (h - 1) * template.getBitmap().getWidth() + w;
        int currentTemplateColor = template.getTemplateColor()[pixelIndex];
        int uiColor = ISDColorUtil.getIsdUIColor(currentTemplateColor, shapeBuilder.getShapeColor());
        color.setColor(uiColor);
        if (length >= template.getTemplateColor().length) {
            //放大：布局与模板比
            color.setWidth((int) (w * template.getTimes()));
            color.setHeight((int) (h * template.getTimes()));
            colorItem.setColorItems((int) (w * template.getTimes()), (int) (h * template.getTimes()), color);
            path.lineTo(w * template.getTimes(), h * template.getTimes());
        } else {
            //缩小：布局与模板比
            color.setWidth((int) (w / template.getTimes()));
            color.setHeight((int) (h / template.getTimes()));
            colorItem.setColorItems((int) (w / template.getTimes()), (int) (h / template.getTimes()), color);
            path.lineTo(w / template.getTimes(), h / template.getTimes());
        }
    }

    @Override
    public void startShape(float x, float y) {
        Log.d(getTag(), "startShape@ " + x + "," + y);
        left = x;
        top = y;
    }

    @Override
    public void stopShape() {
        Log.d(getTag(), "stopShape");
        stop = true;
    }

    @Override
    public ColorItemPool getColorItem(){
        return colorItem;
    }

}
